import { AudioSource, director, Node, AudioClip } from 'cc';
import { ResManager } from '../res/ResManager';

/** 单例模式的音频管理器 */
export class AudioManager {
    private static _instance: AudioManager = null!;
    /** 获取单例的接口 */
    static getInstance() {
        if (this._instance === null) {
            this._instance = new AudioManager();
        }
        return this._instance;
    }

    /** AudioSource 挂载在此节点上 */
    private m_AttachNode: Node = null;
    /** AudioSource 组件 */
    private m_AudioSource: AudioSource = null;
    private constructor() {
        // 私有化的构造函数
        this.m_AttachNode = director.getScene().getChildByName("Canvas");
        this.m_AudioSource = this.m_AttachNode.addComponent(AudioSource);
    }

    /** 默认播放背景音乐接口 */
    /** 默认播放背景音乐接口 */
async playMusic(bUrl: IBundleUrl) {
    // 加载背景音乐后播放
    const audioClip = await gtr.res.loadAssetAsync(bUrl, AudioClip);
    let audioSource = this.m_AudioSource;
    audioSource.clip = audioClip;
    audioSource.loop = true;
    audioSource.play();
}
}

